package rexsee.core.browser;

import java.util.Date;

import org.apache.http.impl.cookie.DateUtils;

import rexsee.core.browser.clazz.JavascriptInterface;
import rexsee.core.utilities.Escape;
import android.content.Context;
import android.net.Uri;
import android.webkit.CookieManager;

public class RexseeCookie implements JavascriptInterface {

	public static final String INTERFACE_NAME = "Cookie";
	@Override
	public String getInterfaceName() {
		return mBrowser.application.resources.prefix + INTERFACE_NAME;
	}
	@Override
	public JavascriptInterface getInheritInterface(Browser childBrowser) {
		return this;
	}
	@Override
	public JavascriptInterface getNewInterface(Browser childBrowser) {
		return new RexseeCookie(childBrowser);
	}

	public final Context mContext;
	private final Browser mBrowser;

	public RexseeCookie(Browser browser) {
		mBrowser = browser;
		mContext = browser.getContext();
	}

	//JavaScript Interface
	public boolean isReady() {
		CookieManager cookieManager = CookieManager.getInstance();
		return cookieManager.acceptCookie();
	}
	public synchronized void enable() {
		CookieManager cookieManager = CookieManager.getInstance();
		cookieManager.setAcceptCookie(true);
	}
	public synchronized void disable() {
		CookieManager cookieManager = CookieManager.getInstance();
		cookieManager.setAcceptCookie(false);
	}
	public boolean isEmpty() {
		CookieManager cookieManager = CookieManager.getInstance();
		return !cookieManager.hasCookies();
	}

	public String get(String url, String key) {
		CookieManager cookieManager = CookieManager.getInstance();
		String cString = cookieManager.getCookie(url);
		if (cString == null) return "";
		String[] cookies = cString.split(";");
		for (int i = 0; i < cookies.length; i++) {
			String[] cookie = cookies[i].split("=");
			if (cookie.length == 2 && cookie[0].trim().equals(key)) { return Escape.unescape(cookie[1]); }
		}
		return "";
	}
	public String get(String key) {
		if (mBrowser.history == null || mBrowser.history.getCurrentItem() == null) return "";
		Uri uri = Uri.parse(mBrowser.history.getCurrentItem().getUrl());
		if (uri == null) return "";
		return get(uri.getHost(), key);
	}
	public String getAll(String url) {
		CookieManager cookieManager = CookieManager.getInstance();
		String cString = cookieManager.getCookie(url);
		if (cString == null) return "{}";
		String[] cookies = cString.split(";");
		String rtn = "";
		for (int i = 0; i < cookies.length; i++) {
			String[] cookie = cookies[i].split("=");
			if (cookie.length == 2) {
				if (!rtn.equals("")) rtn += ",";
				rtn += "\"" + cookie[0].trim() + "\":\"" + Escape.unescape(cookie[1]) + "\"";
			}
		}
		rtn = "{" + rtn + "}";
		return rtn;
	}
	public String getAll() {
		if (mBrowser.history == null || mBrowser.history.getCurrentItem() == null) return "{}";
		Uri uri = Uri.parse(mBrowser.history.getCurrentItem().getUrl());
		if (uri == null) return "{}";
		return getAll(uri.getHost());
	}

	public String getAllKeys(String url) {
		CookieManager cookieManager = CookieManager.getInstance();
		String cString = cookieManager.getCookie(url);
		if (cString == null) return "[]";
		String[] cookies = cString.split(";");
		String rtn = "";
		for (int i = 0; i < cookies.length; i++) {
			String[] cookie = cookies[i].split("=");
			if (cookie.length == 2) {
				if (!rtn.equals("")) rtn += ",";
				rtn += "\"" + cookie[0].trim() + "\"";
			}
		}
		rtn = "[" + rtn + "]";
		return rtn;
	}
	public String getAllKeys() {
		if (mBrowser.history == null || mBrowser.history.getCurrentItem() == null) return "[]";
		Uri uri = Uri.parse(mBrowser.history.getCurrentItem().getUrl());
		if (uri == null) return "[]";
		return getAllKeys(uri.getHost());
	}

	public void set(String url, String key, String value, long expired) {
		String cookie;
		if (expired != 0) {
			Date date = new Date(System.currentTimeMillis() + expired * 1000);
			String GMTString = DateUtils.formatDate(date);
			cookie = key + "=" + Escape.escape(value) + ";expires=" + GMTString;
		} else {
			cookie = key + "=" + Escape.escape(value);
		}
		CookieManager cookieManager = CookieManager.getInstance();
		cookieManager.setCookie(url, cookie);
	}
	public boolean set(String key, String value, long expired) {
		if (mBrowser.history == null || mBrowser.history.getCurrentItem() == null) return false;
		Uri uri = Uri.parse(mBrowser.history.getCurrentItem().getUrl());
		if (uri == null) return false;
		set(uri.getHost(), key, value, expired);
		return true;
	}

	public void remove(String url, String key) {
		set(url, key, "null", -10);
		CookieManager cookieManager = CookieManager.getInstance();
		cookieManager.removeExpiredCookie();
	}
	public void remove(String key) {
		set(key, "null", -10);
		CookieManager cookieManager = CookieManager.getInstance();
		cookieManager.removeExpiredCookie();
	}
	public void removeAll(String url) {
		Date date = new Date(System.currentTimeMillis() - 100000);
		String GMTString = DateUtils.formatDate(date);
		CookieManager cookieManager = CookieManager.getInstance();
		String cString = cookieManager.getCookie(url);
		if (cString == null) return;
		String[] cookies = cString.split(";");
		for (int i = 0; i < cookies.length; i++) {
			cookieManager.setCookie(url, cookies[i].split("=")[0] + "=null;expires=" + GMTString);
		}
		cookieManager.removeExpiredCookie();
	}
	public void removeAll() {
		CookieManager cookieManager = CookieManager.getInstance();
		cookieManager.removeAllCookie();
	}

}
